﻿<?php
class PostsController extends AppController {

	var $name = 'Posts';
	var $helpers = array('Html','Ajax','Javascript','Paginator');
	function danhsachtindang_khongdung() {
		//$this->redirect(array('action' => 'tinvip'));
		$this->Post->recursive = 0;
		$this->set('posts', $this->paginate());
		$this->layout = 'trangchu';
	}

	function view($id = null) {
		$this->layout = 'trangchu';
		if (!$id) {
			$this->Session->setFlash(__('Invalid post', true));
			$this->redirect(array('action' => '/'));
		}
		//lay thong tin cua tin co id
		$post = $this->Post->read(null, $id);
		//cap nhat  luot xem
		$post['Post']['luotxem'] = $post['Post']['luotxem'] + 1;
		$this->Post->save($post);
		$this->set('post', $post);
		//kiem tra nếu là tin vip thì đọc thông tin từ Userpost, ngược lại đọc từ Customerpost
		if($post['Post']['kt_tin']){
			$datas = $this->Userpost->find('all', array( 'conditions' => array( 'Userpost.post_id' => $id),
														//'recursive' => 2,
			));
		}
		else {
			$datas = $this->Customerpost->find('all', array(	'conditions' => array( 'Customerpost.post_id' => $id),
														//	'recursive' => 2,
			));
		}
		$data = $datas[0];
		$this->set('data', $data);
		$data3 = $this->Reply->find('all', array('conditions' => array( 'Reply.post_id' => $id)));
		$this->set('data3', $data3);
		if($this->Session->check("Username")){
			$ten = $this->Session->read("Username");
			$users = $this->User->find('all', array('conditions' => array( 'User.user_name' => $ten),
												'recursive' => -1,
			));		
			$user = $users[0];
			$this->set('user', $user);
		}
	}
	

	function dangtinthanhvien() {
		$this->layout = 'user_hoan';
		$ten = $this->Session->read("Username");
		$users = $this->User->find('all', array('conditions' => array( 'User.user_name' => $ten),
												'recursive' => -1,
		));
		$user = $users[0];
		if (!empty($this->data)) {
			$this->Post->create();
			$this->data['Post']['kt_tin'] = true;
			if ($this->Post->save($this->data)) {
				$userpost= $this->Post->find('first',array(								
								'conditions'=>array('Post.tieude'=> $this->data['Post']['tieude'])));
				$this->Userpost->create();
				$this->Userpost->set(array('post_id' => $userpost['Post']['id'], 'user_id' => $user['User']['id']));
				if($this->Userpost->save()){
					$this->redirect('/');
				}
				else{
					$this->Session->setFlash(__('The userpost could not be saved. Please, try again.', true));
				}
				//$this->Session->setFlash(__('The post has been saved', true));
				
			} else {
				$this->Session->setFlash(__('The post could not be saved. Please, try again.', true));
			}
		}
		$types = $this->Post->Type->find('list');
		$catalogs = $this->Post->Catalog->find('list');
		$this->set(compact('types', 'catalogs'));
	}
	//----------------------------Dang tin khach vang lai--------------
	function dangtinkhach() {
		$this->layout = 'khung_trangchu';
		if (!empty($this->data)) {
			$this->Post->create();
			//-------------luu vao post------------------
			$this->Post->set(array( 'type_id' => $this->data['Post']['type_id'], 
									'catalog_id' => $this->data['Post']['catalog_id'],
									'tieude' => $this->data['Post']['tieude'],
									'noidung' => $this->data['Post']['noidung'],
									'tinhthanh' => $this->data['Post']['tinhthanh'],
									'thoigiantontai' => 10,
									'kt_tin' => false,	
			));
			if($this->Post->save()){
				//--------------luu vao customerpost------------
				$this->Customerpost->create();
				$customerpost= $this->Post->find('first',array(								
								'conditions'=>array('Post.tieude'=> $this->data['Post']['tieude'])));
				$this->Customerpost->set(array(	'post_id' => $customerpost['Post']['id'], 
												'matkhau' => sha1($this->data['Customerpost']['matkhau']),
												'tenlienhe' => $this->data['Customerpost']['tenlienhe'],
												'email'	=> $this->data['Customerpost']['email'],
												'tel' => $this->data['Customerpost']['tel'],
												'yahoo' => $this->data['Customerpost']['yahoo'],	
				));
				if($this->Customerpost->save()){
					$this->redirect('/');
				}
				else{
					$this->Session->setFlash(__('The userpost could not be saved. Please, try again.', true));
				}
			}		
		} else {
				$this->Session->setFlash(__('The post could not be saved. Please, try again.', true));
		}
		//$users = $this->Post->User->find('list');
		$types = $this->Post->Type->find('list');
		$catalogs = $this->Post->Catalog->find('list');
		//$this->set(compact('users', 'types', 'catalogs'));
		$this->set(compact('types', 'catalogs'));
	}
	//-----------------------phần sửa tin của user------------------------------------------
	function suatin($id = null) {
		$this->layout = 'user_hoan';
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid post', true));
			$this->redirect('/');
		}
		if (!empty($this->data)) {
			if ($this->Post->save($this->data)) {
				$this->Session->setFlash(__('The post has been saved', true));
				$this->redirect(array('controller' => 'posts', 'action' => 'view', $this->data['Post']['id']));
			} else {
				$this->Session->setFlash(__('The post could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Post->read(null, $id);
		}
		
		$types = $this->Post->Type->find('list');
		$catalogs = $this->Post->Catalog->find('list');
		$this->set(compact('types', 'catalogs'));
	}
	//------------------------Quyền admin sửa tin --------------------------
	function suatin_admin($id = null) {
		$this->layout = 'user_hoan';
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid post', true));
			$this->redirect('/');
		}
		if (!empty($this->data)) {
			if ($this->Post->save($this->data)) {
				$this->Session->setFlash(__('The post has been saved', true));
				$this->redirect(array('controller' => 'posts', 'action' => 'view', $this->data['Post']['id']));
			} else {
				$this->Session->setFlash(__('The post could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Post->read(null, $id);
		}
		
		$types = $this->Post->Type->find('list');
		$catalogs = $this->Post->Catalog->find('list');
		$this->set(compact('types', 'catalogs'));
		
	}
	function delete($id = null) {
		$this->layout = 'user_hoan';
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for post', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Post->delete($id)) {
			$this->Session->setFlash(__('Post deleted', true));
			$this->redirect('/');
		}
		$this->Session->setFlash(__('Post was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
	function del_post_in_Catalog($id = null, $id2=null){
		$this->Post->delete($id);
		$this->redirect(array('controller' => 'catalogs','action' => 'delete', $id2));
	}
	function tindang_user(){
		$this->layout = 'user_hoan';
		$ten = $this->Session->read("Username");
		$users = $this->User->find('all', array('conditions' => array( 'User.user_name' => $ten),
												'recursive' => -1,
		));
		$user = $users[0];
		// kiểm tra quyền
		if($user['User']['role'] =="admin"){
			$data = $this->Post->find('all');
		}
		else {
			$data =$this->Userpost->find('all', array('conditions' => array('Userpost.user_id' => $user['User']['id'])));
		}
		$this->set("data",$data);
	}
	function index(){}
	function tin1(){
		$this->layout = 'view_layout';
		//$sql2="Select * From posts where vip=1";
		//$data = $this->Post->query($sql2);
		//$this->set("data", $data);
		$data=$this->Post->find('all', array('conditions' => array('Post.vip' => 1),
											'order' => array('Post.created' => 'DESC'),
		));
		return $data;
	}
	function tin2($page){
		$display=5;
		$this->layout = 'view_layout';
		$data2=$this->Post->find('all', array('conditions' => array('Post.vip' => 0),
											  'order' => array('Post.created' => 'DESC'),
											  'limit' => $display,
											  'page' => $page,
		));
		return $data2;
	}
	function tinvip(){
		$this->layout = 'trangchu';
		$display=5;
		//$sql2="Select * From posts where vip=1";
		//$data = $this->Post->query($sql2);
		//$this->set("data", $data);
		$data=$this->Post->find('all', array('conditions' => array('Post.vip' => 1),
											'order' => array('Post.created' => 'DESC'),
		));
		$this->set("data", $data);
		//$sql3 = "Select * From posts where vip=0";
		//$data2 = $this->Post->query($sql3);
		//$this->set("data2", $data2);
		$condition=array();
		$condition=array('Post.vip' => 0);
		$data2=$this->Post->find('all', array('conditions' => array('Post.vip' => 0),											  
											  'limit' => $display,
											  'order' => array('Post.created' => 'DESC')
		));
		//$this->paginate= array(
                //'limit' => $display,
                //'order' => array('Post.created' => 'DESC')
           // );
			//$this->data = $data2;
		$this->set("data2", $data2);
		

		return $display;
	}
	//dem so tin thuong trong tat ca
	function demthuong(){
		$thuong=$this->Post->find('count', array(
												'conditions' => array('Post.vip' => 0)
												));
		return $thuong;
	}
	function search() {
		$this->layout = 'trangchu' ;
		if($this->data['Post']['tieude']!=null){
		//neu co thi truy van du lieu dua vao bien $users
			$posts=$this->Post->find('all',array('conditions'=>array('tieude LIKE '=>'%'.$this->data['Post']['tieude'].'%')));
		//goi du lieu tu controller len view
			$this->set('posts',$posts);
		}
		else {
			$posts = null;
			$this->set('posts',$posts);
		}
	}
	function demtin(){
		$dem = $this->Post->find('count');
		return $dem;
	}
}
